<?php


namespace app\hapi\controller;

use app\api\controller\CarAct;
use app\common\controller\Tree;
use app\common\model\AuthGroup;
use app\common\model\AuthRule;
use app\common\model\Moncar;
use app\common\model\TicketNo;
use etpcar\EtpCar;
use think\Cache;
use think\Controller;
use think\Request;

class Api extends ApiBase
{
    public function api(Request $request)
    {
//        return json($token);
        if ($request->isPost()) {
            $param = $request->param();
            $querytoken = $this->querytoken($param, $request->header('x-token'));
            if ($querytoken['return_code'] == "token_error") {
                return json($querytoken);
            }else{
                $param['token']=$request->header('x-token');
            }
            switch ($param['method']) {
                case "getlogin":
                    $re_data = action('hapi/login/getlogin', array($param));
                    break;
                case "logout":
                    $re_data = action('hapi/login/logout', array($param));
                    break;
                case "getuserinfo":
                    $re_data = action('hapi/login/getuserinfo', array($param));
                    break;
                case "getparklist":
                    $re_data = action('hapi/parking/getparklist', array($param));
                    break;
                case "parkadd":
                    $re_data = action('hapi/parking/parkadd', array($param));
                    break;
                case "parkedit":
                    $re_data = action('hapi/parking/parkedit', array($param));
                    break;
                case "parkdel":
                    $re_data = action('hapi/parking/parkdel', array($param));
                    break;
                case "getparklog":
                    $re_data = action('hapi/parking/parkloglist', array($param));
                    break;
                case "getparklanelist":
                    $re_data = action('hapi/parking/getparklanelist', array($param));
                    break;
                case "parklaneadd":
                    $re_data = action('hapi/parking/parklaneadd', array($param));
                    break;
                case "parklaneedit":
                    $re_data = action('hapi/parking/parklaneedit', array($param));
                    break;
                case "parklanedel":
                    $re_data = action('hapi/parking/parklanedel', array($param));
                    break;
                case "getparkloginfo":
                    $re_data = action('hapi/parking/getparkloginfo', array($param));
                    break;
                case "getauthgroup":
                    $re_data = action('hapi/auth/getauthgroup', array($param));
                    break;
                case "authgroupadd":
                    $re_data = action('hapi/auth/authgroupadd', array($param));
                    break;
                case "authgroupedit":
                    $re_data = action('hapi/auth/authgroupedit', array($param));
                    break;
                case "authgroupdel":
                    $re_data = action('hapi/auth/authgroupdel', array($param));
                    break;
                case "getroutes":
                    $re_data = action('hapi/auth/getroutes', array($param));
                    break;
                case "getmenulist":
                    $re_data = action('hapi/auth/getmenulist', array($param));
                    break;
                case "menuadd":
                    $re_data = action('hapi/auth/menuadd', array($param));
                    break;
                case "menuedit":
                    $re_data = action('hapi/auth/menuedit', array($param));
                    break;
                case "menudel":
                    $re_data = action('hapi/auth/menudel', array($param));
                    break;
                case "getmenuinfo":
                    $re_data = action('hapi/auth/getmenuinfo', array($param));
                    break;
                case "getuserlist":
                    $re_data = action('hapi/sysusers/getuserlist', array($param));
                    break;
                case "sysuseradd":
                    $re_data = action('hapi/sysusers/sysuseradd', array($param));
                    break;
                case "sysuseredit":
                    $re_data = action('hapi/sysusers/sysuseredit', array($param));
                    break;
                case "sysuserdel":
                    $re_data = action('hapi/sysusers/sysuserdel', array($param));
                    break;
                case "getmoncarlist":
                    $re_data = action('hapi/caradmin/moncarlist', array($param));
                    break;
                case "moncaradd":
                    $re_data = action('hapi/caradmin/moncaradd', array($param));
                    break;
                case "moncaredit":
                    $re_data = action('hapi/caradmin/moncaredit', array($param));
                    break;
                case "moncardel":
                    $re_data = action('hapi/caradmin/moncardel', array($param));
                    break;
                case "carticketlist":
                    $re_data = action('hapi/caradmin/carticketlist', array($param));
                    break;
                case "ticketnoadd":
                    $re_data = action('hapi/caradmin/ticketnoadd', array($param));
                    break;
                case "ticketnoedit":
                    $re_data = action('hapi/caradmin/ticketnoedit', array($param));
                    break;
                default:
                    $re_data = ['return_code' => "FAIL", 'return_msg' => "请求不存在"];
            }
            return json($re_data);
        }

    }

    private function querytoken($param, $token)
    {
        if ($param['method'] !== "getlogin" && $param['method'] !== "logout") {
            $token = Cache::store('redis')->get($token);
            if (!$token) {
                $re_param['return_code'] = "token_error";
                $re_param['return_msg'] = "登录状态异常";

            } else {
                $re_param['return_code'] = "SUCCESS";
                $re_param['return_msg'] = "";
            }
            return $re_param;

        } else {
            $re_param['return_code'] = "SUCCESS";
            $re_param['return_msg'] = "";
        }
        return $re_param;


    }

    public function test123()
    {
        $data['carno'] = "豫E1M9261";
        $data['StartTime'] = "20190424210000";
        $option = [
            'key' => "13E016AC",
            'parkno' => "18568816812",
        ];
        $EptCar = new EtpCar($option);
        $res = $EptCar->get_pay_list($data);
        var_dump($res);
        exit();

        if ($res['ReMsg']['ErrNo'] == "0000") {
            $ticketinfo = [];
            if (isset($res['PayList']['PayInfo'])) {
                $payinfo = $res['PayList']['PayInfo'];
                foreach ($payinfo as $k => $v) {
                    if ($v['TicketNo']) {
                        $ticketinfo[$k] = $v['TicketNo'];
                    }
                }
            }
        }
        if(isset($ticketinfo)){
            $map['ticket_no']=array('in',$ticketinfo);
            $datas['status']="Y";
            $datas['use_time']=date('Y-m-d H:i:s');
            TicketNo::where($map)->update($datas);
        }


    }

    public function upmoncar()
    {
        // [Description("新增")]
        //            Add,
        //            [Description("删除")]
        //            Del,
        //            [Description("修改")]
        //            Edit,
        //            [Description("延期")]
        //            Delay,
        $data['CarNo'] = "豫E1M926";
        $data['UserName'] = "李世正";
        $data['OperType'] = "del";
        $data['Fee'] = "0";
        $data['StartTime'] = date('YmdHis');
        $data['EndTime'] = date('YmdHis', strtotime('+5 day'));
        $data['CarType'] = "月卡B";
        $data['CardStatus'] = "5";
        $data['Balance'] = "0";
        $data['MachList'] = "53_54";
        $data['SetNo'] = "";
        $option = [
            'key' => "13E016AC",
            'parkno' => "18568816812",
        ];
        $EptCar = new EtpCar($option);
        $res = $EptCar->up_mon_car($data);
        var_dump($res);
    }

    public function test222()
    {
        $str = '{"parkid":"1","username":"李世正","carno":"豫E1M926","phone":"18568816812","starttime":"2019-04-23 00:00:00","endtime":"2019-04-30 00:00:00","dist_type":"1","dist_fee":""}';
        $query = json_decode($str, true);
        $s_data['parkid'] = $query['parkid'];
        $s_data['carno'] = $query['carno'];
        $s_data['username'] = $query['username'];
        $s_data['phone'] = $query['phone'];
        $s_data['starttime'] = $query['starttime'];
        $s_data['endtime'] = $query['endtime'];
        $s_data['dist_type'] = $query['dist_type'];
        $s_data['dist_fee'] = $query['dist_fee'];
        var_dump($s_data);
        $CarAct = new CarAct();
        $res = $CarAct->carticketnoact($s_data, "Edit");
        var_dump($res);

    }

    public function upcompany()
    {
//        $data['OperateType'] = "Add";
//        $data['LinkName'] = "测试";
//        $data['CompanyName'] = "测试商家";
//        $data['CustomerID']="Add";
//        $data['Phone']="Add";
//        $data['Adress']="Add";
//        $data['AccountDate']="Add";
//        $data['AccountType']="Add";
//        $data['MachNo']="Add";
//        $data['FreeType']="Add";
//        $data['CreateTime']="Add";
//        $data['Status']="Add";
//        $data['LocalMoney']="Add";
//        $data['LocalTime']="Add";
//        $data['IsOverPay']="Add";
//        $data['IsInvite']="Add";
//        $data['EndTime']="Add";
//        $data['IsTicketNo']="Add";
//        $data['Minutes']="Add";
//        $data['ValType']="Add";
//        $data['Cflg']="Add";
//        $data['EndTime']="Add";
//        $data['EndTime']="Add";
//        $data['EndTime']="Add";
//        $data['EndTime']="Add";
//        $data['OperateType']="Add";

        $data['VstName'] = "李世正";
        $data['CarNo'] = "豫E1M926";
        $data['VstPhone'] = "18568816812";
        $data['StartTime'] = date('YmdHis');
        $data['EndTime'] = date('YmdHis', strtotime('+3 day'));
        $option = [
            'key' => "13E016AC",
            'parkno' => "18568816812",
        ];
        $EptCar = new EtpCar($option);
        $res = $EptCar->up_visitor($data);
        var_dump($res);
    }

    public function test()
    {
        $data['OperateType'] = "Add";
        $data['CarNo'] = "豫E1M926";
        $data['DistType'] = "1";
        $data['StartTime'] = date('YmdHis');
        $data['EndTime'] = date('YmdHis', strtotime('+1 day'));
        $option = [
            'key' => "13E016AC",
            'parkno' => "18568816812",
        ];
        $EptCar = new EtpCar($option);
        $res = $EptCar->up_ticket_no($data);
        var_dump($res);

    }

    public function up_active()
    {
        $data['OperateType'] = "Add";
//        $data['Subject']="Add";
//        $data['Memo']="Add";
        $data['ManCnt'] = "100";
        $data['DistType'] = "1";
//        $data['DistFee']="100-1:30";
        $data['StartTime'] = date('YmdHis');
        $data['EndTime'] = date('YmdHis', strtotime('+7 day'));
        $data['CreateTime'] = date('YmdHis');
        $data['CustomerID'] = "30001";
//        $data['Address']="Add";
//        $data['ActiveType']="1";
        $option = [
            'key' => "13E016AC",
            'parkno' => "18568816812",
        ];
        $EptCar = new EtpCar($option);
        $res = $EptCar->up_active($data);
        var_dump($res);

    }

    public function getsellerlist()
    {
        $option = [
            'key' => "13E016AC",
            'parkno' => "18568816812",
        ];
        $EptCar = new EtpCar($option);
        $res = $EptCar->get_sellerlist([]);
        var_dump($res);

    }

    public function test2()
    {
        $option = [
            'key' => "13E016AC",
            'parkno' => "18568816812",
        ];
        $EptCar = new EtpCar($option);

        $param['carNo'] = "豫E1M926";
        $param['carType'] = "H";
//        $param['PayFee'] = 1;
        $param['MachNo'] = "53";
        $param['Memo'] = "测试";
        $param['OpenFlg'] = "0";
        $RES = $EptCar->up_open_cmd($param);
        var_dump($RES);

    }

    private function t($list, $id)
    {
        $arr = array();
        foreach ($list as $v) {
            if ($v['id'] == $id) {
                $arr[] = $v;// $arr[$v['id']]=$v['name'];
                $arr = array_merge(self::t($list, $v['pid']), $arr);
            }
        }
        return $arr;
    }

}